//
//  FansRedPaperViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/2/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FansRedPaperViewController.h"
#import "FansPaperTopView.h"
#import "FansRedPaperCell.h"
#import "FansPackListModel.h"

@interface FansRedPaperViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSMutableArray *_dataArray;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *total;
@end

@implementation FansRedPaperViewController

#pragma mark -- 收到红包请求列表
- (void)RedPaperRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kFansPackList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            self.total = KString(responseObject[@"data"][@"obtain"]);
            for (NSDictionary *dic in array) {
                FansPackListModel *model = [[FansPackListModel alloc] initWithDict:dic];
                [_dataArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收到的红包";
    self.total = @"0";
    [self RedPaperRequest];
    [self setUpUI];
    // Do any additional setup after loading the view.
}

#pragma mark -- UI
- (void)setUpUI {
    _dataArray = [NSMutableArray arrayWithCapacity:1];
    //tableView
    [self creatTableView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FansRedPaperCell *cell = [FansRedPaperCell loadNibCellWithTabelView:tableView];
    FansPackListModel *model = _dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -- 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    FansPaperTopView *topView = [[NSBundle mainBundle] loadNibNamed:@"FansPaperTopView" owner:self.tableView options:nil].lastObject;
    topView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 170);
    topView.totalPacks.text = self.total;
    return topView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 170;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 57;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
